package org.sdsc.crms.web.security;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import org.sdsc.crms.entity.security.Dept;
import org.sdsc.crms.service.security.DeptManager;
import org.sdsc.crms.web.CrudActionSupport;
import org.sdsc.modules.web.struts2.Struts2Utils;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("serial")
public class DeptAction extends CrudActionSupport<Dept> {

	@Autowired
	private DeptManager deptManager;

	private Dept entity;

	private Long id;

	public Dept getModel() {
		return entity;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = deptManager.get(id);
		} else {
			entity = new Dept();
		}

	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String searchByParentId() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		String parentId = request.getParameter("id");
		List<Dept> deptList = deptManager.getDeptByParentId(Long
				.parseLong(parentId));
		String jsonString = "";
		if (!deptList.isEmpty()) {
			jsonString = new Dept().fromListToJsonString(deptList);
		}
		Struts2Utils.renderJson(jsonString);
		return null;
	}

	public String searchById() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		Dept dept = null;
		String id = request.getParameter("id");
		if (id != null && !id.equals("")) {
			dept = deptManager.get(Long.parseLong(id));

		} else {
			dept = new Dept();
		}
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] { "childDepts", "parentDept" });
		JSONObject jsonDept = (JSONObject) JSONSerializer.toJSON(dept,
				jsonConfig);
		Struts2Utils.renderJson(jsonDept.toString());
		System.out.println(jsonDept.toString());
		return null;
	}

	@Override
	public String save() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		String parentId = request.getParameter("parentId");
		if (parentId != null && !parentId.equals("")) {
			Dept parentDept = deptManager.get(Long.parseLong(parentId));
			entity.setParentDept(parentDept);
		}
		deptManager.save(entity);
		return null;
	}

	@Override
	public String delete() throws Exception {
		String jsonString = "{";
		try {
			deptManager.delete(id);
			jsonString += " msg: '删除成功' }";
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			jsonString += " msg: '删除失败' }";
		}
		Struts2Utils.renderJson(jsonString);
		return null;
	}

	public String move() throws Exception {
		HttpServletRequest request = Struts2Utils.getRequest();
		String id = request.getParameter("id");
		String parentId = request.getParameter("parentId");
		Dept parentDept = deptManager.get(Long.parseLong(parentId));
		Dept dept = deptManager.get(Long.parseLong(id));
		dept.setParentDept(parentDept);
		deptManager.save(dept);
		Struts2Utils.renderJson("{ msg: '操作成功' }");
		return null;
	}

	// 不需要input方法
	@Override
	public String input() throws Exception {
		return null;
	}

	// 由于重写了execute方法,所以不需要list方法
	@Override
	public String list() throws Exception {
		return null;
	}

}
